class HistoryController < ApplicationController
	layout "m"
  def index
	  @action_items = ActionItem.find(:all)
    @projects = Project.find(:all)
  end
	def right_bar
    @actions = ActionItem.find(:all, :conditions=>["status!=0"])
    @actions.delete_if{|x| x.project.status == 0}
    @projects = Project.find(:all,:conditions=>["status!=0"])
    @contacts = Contact.find(:all,:conditions=>["status!=0"])
    @lineitems = LineItem.find(:all, :conditions=>["status!=0"])
    @lineitems.delete_if{|x| x.fund.status == 0}
    @funds = Fund.find(:all,:conditions=>["status!=0"])
    @users = User.find(:all,:conditions=>["status!=0"])
    render :layout=> false
  end
  def ajax_search
    if params[:search][:input].length() > 0
      @varinput = "%#{params[:search][:input]}%"
      @search_list = ActionItem.find(:all,:conditions => ["shead LIKE ? AND status != 0",@varinput])
      @search_list += Contact.find(:all,:conditions => ["shead LIKE ? AND status != 0",@varinput])
      @search_list += Project.find(:all,:conditions => ["shead LIKE ? AND status != 0",@varinput])
      @search_list += Fund.find(:all,:conditions => ["shead LIKE ? AND status != 0",@varinput])
      @search_list += LineItem.find(:all,:conditions => ["shead LIKE ? AND status != 0",@varinput])
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
      
      if @search_list.length() > 0
        @show = true
      end
    else
      @show = false
    end
    @input = params[:search][:input].to_s
    render :partial => 'ajax_search', :locals => { :@search_list => @search_list }
  end
  def search_results
    # body search terms
    if params[:search][:scope] == "Body" and params[:search][:status] == "Open"
      @conditions = ["sbody LIKE ? AND status != 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Body" and params[:search][:status] == "Closed"
      @conditions = ["sbody LIKE ? AND status = 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Body" and params[:search][:status] == "All"
      @conditions = ["sbody LIKE ?", "%#{params[:search][:input]}%"]
    # title search terms
    elsif params[:search][:scope] == "Title" and params[:search][:status] == "Open"
      @conditions = ["shead LIKE ? AND status != 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Title" and params[:search][:status] == "Closed"
      @conditions = ["shead LIKE ? AND status = 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Title" and params[:search][:status] == "All"
      @conditions = ["shead LIKE ?", "%#{params[:search][:input]}%"]
    # both search terms
    elsif params[:search][:scope] == "Both" and params[:search][:status] == "Open"
      @conditions = ["shead LIKE ? AND status != 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Both" and params[:search][:status] == "Closed"
      @conditions = ["shead LIKE ? AND status = 0", "%#{params[:search][:input]}%"]
    elsif params[:search][:scope] == "Both" and params[:search][:status] == "All"
      @conditions = ["shead LIKE ? OR sbody LIKE ?", "%#{params[:search][:input]}%","%#{params[:search][:input]}%"]
    end
    if params[:search][:kind] == "Action Items"
      @search_list = ActionItem.find(:all, :conditions => @conditions )
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
    elsif params[:search][:kind] == "Projects"
      @search_list = Project.find(:all,:conditions => @conditions)
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
    elsif params[:search][:kind] == "Contacts"
      @search_list = Contact.find(:all,:conditions => @conditions)
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
    elsif params[:search][:kind] == "Funds"
      @search_list = Fund.find(:all,:conditions => @conditions)
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
    elsif params[:search][:kind] == "Line Items"
      @search_list = LineItem.find(:all,:conditions => @conditions)
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]} 
    else
      @search_list = ActionItem.find(:all,:conditions => @conditions)
      @search_list += Contact.find(:all,:conditions => @conditions)
      @search_list += Project.find(:all,:conditions => @conditions)
      @search_list += Fund.find(:all,:conditions => @conditions)
      @search_list += LineItem.find(:all,:conditions => @conditions)
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase] }
    end
    @input = params[:search][:input].to_s
    render :partial => 'search_results', :locals => { :@search_list => @search_list, :@input => @input }
  end
  def search
    if request.post?
      @varinput = "%#{params[:search][:input]}%"
      @search_list = ActionItem.find(:all,:conditions => ["shead LIKE ?",@varinput])
      @search_list += Contact.find(:all,:conditions => ["shead LIKE ?",@varinput])
      @search_list += Project.find(:all,:conditions => ["shead LIKE ?",@varinput])
      @search_list += Fund.find(:all,:conditions => ["shead LIKE ?",@varinput])
      @search_list += LineItem.find(:all,:conditions => ["shead LIKE ?",@varinput])
      @search_list = @search_list.sort_by{|x| [x[:shead].downcase]}
      @input = params[:search][:input].to_s
    end
  end
  def cleanhistory
    @action_items = ActionItem.find(:all)
    @projects = Project.find(:all)
    @contacts = Contact.find(:all)
    @funds = Fund.find(:all)
    @line_items = LineItem.find(:all)
    @action_items.each do |c|
      c.shead = c.first_entry.name
      c.sbody = c.first_entry.note
      c.smeta = c.first_entry.note
      c.save!
    end
    @projects.each do |c|
      c.shead = c.first_entry.name
      c.sbody = c.first_entry.note
      c.smeta = c.first_entry.note
      c.save!
    end
    @contacts.each do |c|
      c.shead = c.first_entry.fname + " " + c.first_entry.lname
      c.sbody = c.first_entry.note
      c.smeta = c.first_entry.note
      c.save!
    end
    @funds.each do |c|
      c.shead = c.first_entry.name
      c.sbody = c.first_entry.note
      c.smeta = c.first_entry.note
      c.save!
    end
    @line_items.each do |c|
      c.shead = c.first_entry.name
      c.sbody = c.first_entry.note
      c.smeta = c.first_entry.note
      c.save!
    end
    redirect_to :back
  end
end
